package com.chenxinzhimusic.page

import android.os.Build
import androidx.annotation.RequiresApi
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.interaction.Interaction
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListItemInfo
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.DragHandle
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.chenxinzhimusic.data.loveKey
import com.chenxinzhimusic.data.playListKey
import com.chenxinzhimusic.data.saveDataContent
import com.chenxinzhimusic.refreshFlow
import com.chenxinzhimusic.viewmodel.page.PlayListViewModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.launch
import org.koin.androidx.compose.koinViewModel
import sh.calvin.reorderable.ReorderableItem
import sh.calvin.reorderable.rememberReorderableLazyListState

@RequiresApi(Build.VERSION_CODES.TIRAMISU)
@Composable
fun PlayList(
    sharedFlow: MutableSharedFlow<Unit>,
    playListViewModel: PlayListViewModel = koinViewModel()
) {
    val rememberCoroutineScope = rememberCoroutineScope()
    Column(modifier = Modifier.fillMaxSize()) {
        var nowIndex by remember { mutableIntStateOf(0) }
        val pagerState =
            rememberPagerState(initialPage = 0, pageCount = { playListViewModel.type.size })
        LaunchedEffect(pagerState.currentPage) {
            nowIndex = pagerState.currentPage
        }
        Spacer(modifier = Modifier.height(10.dp))
        Row(
            modifier = Modifier
                .padding(horizontal = 20.dp)
                .fillMaxWidth()
        ) {
            playListViewModel.type.forEachIndexed { index, it ->
                Text(
                    it,
                    color = if (nowIndex == index) Color.White else Color.White.copy(0.5f),
                    fontSize = 20.sp,
                    modifier = Modifier.pointerInput(
                        Unit
                    ) {
                        detectTapGestures {
                            rememberCoroutineScope.launch {
                                pagerState.animateScrollToPage(index)
                            }

                        }
                    })
                Spacer(modifier = Modifier.width(10.dp))
            }
        }
        Spacer(modifier = Modifier.height(10.dp))

        var loveList = playListViewModel.loveList
        var playListD = playListViewModel.playListD

        LaunchedEffect(Unit) {
            playListViewModel.loveList = null
            playListViewModel.playListD = null
            refreshFlow.emit(Unit)
        }
        var list =
            listOf(loveList to { from: LazyListItemInfo, to: LazyListItemInfo ->
                playListViewModel.loveList = loveList!!.copy().apply {
                    musicList =
                        musicList.toMutableList().apply {
                            add(to.index, removeAt(from.index))
                        }
                }
            }, playListD to { from: LazyListItemInfo, to: LazyListItemInfo ->
                playListViewModel.playListD = playListD!!.copy().apply {
                    musicList =
                        musicList.toMutableList().apply {
                            add(to.index, removeAt(from.index))
                        }
                }

            })
        val playListState = playListViewModel.type.mapIndexed { index, it ->
            var lazyListState = rememberLazyListState()
            val rememberReorderableLazyListState =
                rememberReorderableLazyListState(lazyListState) { from, to ->
                    list[index].second(from, to)

                }
            lazyListState to rememberReorderableLazyListState
        }
        val key = listOf(loveKey, playListKey)
        val context = LocalContext.current
        HorizontalPager(state = pagerState) {
            LazyColumn(
                state = playListState[it].first,
                modifier = Modifier
                    .fillMaxSize()
            ) {
                itemsIndexed((list[it].first?.musicList) ?: listOf(), key = { index, item ->
                    item.musicId
                }) { index, item ->
                    ReorderableItem(playListState[it].second, item.musicId) { isDrag ->
                        LaunchedEffect(isDrag) {
                            if (!isDrag) {
                                //保存新的拖动数据
                                context.saveDataContent(key[it], list[it].first!!)
                            }
                        }
                        val a by animateDpAsState(if (isDrag) 2.dp else 0.0.dp)
                        MusicItemView(
                            item, list[it].first!!, index, it == 1, playListState[it].first,
                            modifier = Modifier,
                            shadowSetting = {
                                shadow(a)
                            }
                        ) {
                            IconButton(
                                modifier = Modifier
                                    .draggableHandle()
                                    .size(50.dp),
                                onClick = {},
                                interactionSource = remember {
                                    object : MutableInteractionSource {
                                        override suspend fun emit(interaction: Interaction) {

                                        }

                                        override fun tryEmit(interaction: Interaction): Boolean {
                                            return true
                                        }

                                        override val interactions: Flow<Interaction>
                                            get() = MutableSharedFlow<Interaction>(
                                            )

                                    }
                                }

                            ) {
                                Icon(
                                    Icons.Rounded.DragHandle,
                                    contentDescription = "拖动",
                                    tint = Color.White
                                )
                            }
                        }


                    }
                }
            }

        }
    }

}